{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## List"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__add__',\n",
       " '__class__',\n",
       " '__contains__',\n",
       " '__delattr__',\n",
       " '__delitem__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__getitem__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__iadd__',\n",
       " '__imul__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__iter__',\n",
       " '__le__',\n",
       " '__len__',\n",
       " '__lt__',\n",
       " '__mul__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__reversed__',\n",
       " '__rmul__',\n",
       " '__setattr__',\n",
       " '__setitem__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " 'append',\n",
       " 'clear',\n",
       " 'copy',\n",
       " 'count',\n",
       " 'extend',\n",
       " 'index',\n",
       " 'insert',\n",
       " 'pop',\n",
       " 'remove',\n",
       " 'reverse',\n",
       " 'sort']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir([])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 添加元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`append(ele)`  尾部添加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张三', '李四']\n"
     ]
    }
   ],
   "source": [
    "users = []\n",
    "users.append(\"张三\")\n",
    "users.append(\"李四\")\n",
    "print(users)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`insert(i, ele)`指定位置添加元素, 如果位置为负数, 表示相对于尾部的位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['王大', '张三', '李四']\n",
      "['王大', '张三', '赵小六', '李四']\n"
     ]
    }
   ],
   "source": [
    "users = [\"张三\", \"李四\"]\n",
    "users.insert(0,\"王大\")\n",
    "print(users)\n",
    "\n",
    "users.insert(-1, \"赵小六\")\n",
    "print(users)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删除元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`pop([index=-1])` 删除并返回指定位置的元素, 默认为 -1, 删除最后一个元素; 如果指定索引不存在 或者 列表为空 将会抛出错误"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "李四\n"
     ]
    }
   ],
   "source": [
    "users = ['王大', '张三', '赵小六', '李四']\n",
    "# print(users.pop(6))\n",
    "print(users.pop())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`remove(ele)` 删除首次出现的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "[36, 89, 25, 18]\n"
     ]
    }
   ],
   "source": [
    "ages = [18, 36, 89, 25, 18]\n",
    "ages.remove(18)\n",
    "print(ages)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 修改元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过索引修改元素的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[19, 36, 89, 25, 18]\n"
     ]
    }
   ],
   "source": [
    "ages = [18, 36, 89, 25, 18]\n",
    "ages[0] = 19\n",
    "print(ages)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查找元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`index(ele)` 返回列表中指定元素首次出现的索引; 如果元素不存在, 则抛出错误"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "ages = [18, 36, 89, 25, 18]\n",
    "print(ages.index(18))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用 `in` 判断元素是否存在于列表中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "元素 20 不存在与列表中\n"
     ]
    }
   ],
   "source": [
    "ages = [18, 36, 89, 25, 18]\n",
    "\n",
    "if 20 in ages:\n",
    "    print(\"元素 20 的索引是: \", ages.index(20))\n",
    "else:\n",
    "    print(\"元素 20 不存在与列表中\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`count()` 统计列表中指定元素出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "ages = [18, 36, 89, 25, 18]\n",
    "print(ages.count(18))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "访问元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age_1 = ages[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 其他"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`reserve()` 翻转 列表中的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 4, 3, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "ages = [1, 2, 3, 4, 5]\n",
    "ages.reverse()\n",
    "print(ages)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用 + 合并数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12, 14, 18, 19, 20, 45, 18]\n"
     ]
    }
   ],
   "source": [
    "age_1 = [12, 14, 18]\n",
    "age_2 = [19, 20, 45, 18]\n",
    "ages = age_1 + age_2\n",
    "print(ages)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数组去重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12, 45, 14, 18, 19, 20]\n"
     ]
    }
   ],
   "source": [
    "ages = [12, 14, 18, 19, 20, 45, 18]\n",
    "ages_unique = list(set(ages))\n",
    "print(ages_unique)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数组切片\n",
    "\n",
    "- `[:]` 数组从头到尾\n",
    "- `[1:3]` 数组索引第 1 个元素到第三个元素, 不包括第三个元素\n",
    "- `[2:]` 数组索引第 2 个元素到最后一个元素\n",
    "- `[:3]` 数组索引从开始到 第 3 个元素, 不包括第三个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12, 14, 18, 19, 20, 45, 18]\n",
      "[14, 18]\n",
      "[18, 19, 20, 45, 18]\n",
      "[12, 14, 18]\n"
     ]
    }
   ],
   "source": [
    "ages = [12, 14, 18, 19, 20, 45, 18]\n",
    "\n",
    "print(ages[:])\n",
    "print(ages[1:3])\n",
    "print(ages[2:])\n",
    "print(ages[:3])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`copy()` 复制数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12, 14, 18, 19, 20, 45, 18]\n"
     ]
    }
   ],
   "source": [
    "ages = [12, 14, 18, 19, 20, 45, 18]\n",
    "ages_copy = ages.copy()\n",
    "print(ages_copy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`clear()` 清空数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "ages = [12, 14, 18, 19, 20, 45, 18]\n",
    "ages.clear()\n",
    "print(ages)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
